﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using NHibernate;
using ZLCMS.Business;

namespace ZLCMS.Web.SystemManage.Basics.Kind
{
    public partial class Edit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);
            if (IsPostBack)
            {
                SaveKind();
            }
            else
            {
                InitializeContent();
            }
        }

        protected void InitializeContent()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ddlConfig.DataSource = session.CreateCriteria<CmsDocConfig>().List<CmsDocConfig>();
                ddlConfig.DataTextField = "Id";
                ddlConfig.DataValueField = "Id";
                ddlConfig.DataBind();

                String Id = Request["uid"];
                KindId.Value = Id;

                CmsKindInfo kind = session.Get<CmsKindInfo>(Id);

                ddlConfig.SelectedValue = kind.ConfigId;
                txtKindId.Text = kind.Id;
                txtKindName.Text = kind.KindText;
                txtManagePage.Text = kind.ManagePage;
                txtListPage.Text = kind.ListPage;
                txtDetailPage.Text = kind.DetailPage;
            }
        }

        protected void SaveKind()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CmsKindInfo kind = session.Get<CmsKindInfo>(KindId.Value);
                kind.Id = txtKindId.Text;
                kind.ConfigId = ddlConfig.SelectedValue;
                kind.KindText = txtKindName.Text;
                kind.ManagePage = txtManagePage.Text;
                kind.ListPage = txtListPage.Text;
                kind.DetailPage = txtDetailPage.Text;

                session.Update(kind);
                session.Flush();
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = _framePage.NavTabId
            });
        }
    }
}
